Feedback
The Havok Xtra can be a rich source of information beyond the position and orientation of rigid bodies. At any time we can get a body's velocity or how much force is being applied to it. We can also get information regarding rigid body collisions. To finish our example scene we will add a section of Lingo script, which plays a sound each time the ball strikes the box. The code to do this comes in two sections. First we must inform the Havok Xtra that we are interested in collisions between the ball and the box rigid bodies.
on beginSprite me
w = member( 3 )
hk = member( 4 )
w.resetWorld()
hk.initialize( w, 0.1, 1 )
createVisibleObjects()
createPhysicalObjects()
createDashpot()
createSpring()
hk.registerInterest( "TheBox", "TheBall", 10, 0, #collision, me )end
The script above shows the final version of the beginSprite function. The line shown in bold is where interest in the collision is declared. When a collision between "TheBox" and "TheBall" occurs the second section of code (the collision function) will be invoked. The script for the "collision" function is below.
Note: The code below assumes that the sound file to be played in stored as the movie cast member "Sound".
on collision me, details
sound(1).volume = integer( 255 / power( details[5], 1.1 ) )
sound(1).play( member( "Sound" ) )end
As can be scene this collision callback function accepts a parameter called "details". This parameter contains all the information known about the collision including names of the rigid bodies involved, the point at which the collision occurred and relative direction of the collision. The final piece of information passed to the callback is a value representing how strong the collision was. In the above example we use this value to assign a volume to the sound we are to play. This means that the harder the ball strikes the box then the louder the sound.